package com.validant.enigma3.samples;

import org.testng.annotations.Test;

import com.validant.enigma3.common.TestBase;
import com.validant.enigma3.controls.ExtWebElement;

public class FileUpload extends TestBase {

	// form Basic File Field
	private static final String CTRL_UPLOAD_FILE = "File Upload Field";
	private static final String CTRL_GET_FILE_PATH = "Get File Path";
	private static final String CTRL_SELECTED_FILE = "Selected File";
	
	// form Form Example
	private static final String CTRL_NAME = "Name";
	private static final String CTRL_PHOTO = "Photo";
	private static final String CTRL_SAVE = "Save";
	private static final String CTRL_SUCC_MSG = "Success Message";
	private static final String CTRL_ICON = "Icon";
	
	/*
	 * A sample about selecting file uploading
	 * Issue: upload file field is read-only, so cannot send keys
	 * Solution: the script removes READONLY attribute first, then send keys
	 */
	@Test(description = "Basic File Field")
	public void BasicFileField()  
	{
		String uploadFile = "/path/to/file";

		// enter uploading file
		ExtWebElement uploadField = new ExtWebElement(driver.waitForElementVisible(uimap.getElement(CTRL_UPLOAD_FILE)));
		uploadField.removeAttribute(driver,"readonly");
		uploadField.sendKeys(uploadFile);
		
		// verify entered data
		uploadField.verifyAttributeEquals("value", uploadFile);
		
		// Click Get File Path
		ExtWebElement getFilePath = new ExtWebElement(driver.waitForElementVisible(uimap.getElement(CTRL_GET_FILE_PATH)));
		getFilePath.click();
		
		ExtWebElement selectedFile = new ExtWebElement(driver.waitForElementVisible(uimap.getElement(CTRL_SELECTED_FILE)));
		selectedFile.verifyTextContains(uploadFile);
	}
	
	@Test(description = "File Upload Form")
	public void FileUploadForm()  
	{
		/*
		 * Notice: there is a bug in this tests
		 */
		
		String file = "C:/Windows/notepad.exe";
		
		// enter data
		ExtWebElement name = new ExtWebElement(driver.waitForElementVisible(uimap.getElement(CTRL_NAME)));
		name.sendKeys("Notepad");
		
		ExtWebElement photo = new ExtWebElement(driver.waitForElementVisible(uimap.getElement(CTRL_PHOTO)));
		photo.removeAttribute(driver,"readonly");
		photo.sendKeys(file);		
		
		// click save
		ExtWebElement save = new ExtWebElement(driver.waitForElementVisible(uimap.getElement(CTRL_SAVE)));
		save.click();
		
		// wait for message dialog occurs
		driver.waitForElementVisible(uimap.getElement(CTRL_ICON));
		
		// verify successful message
		ExtWebElement successMsg = new ExtWebElement(driver.waitForElementVisible(uimap.getElement(CTRL_SUCC_MSG)));
		successMsg.verifyTextContains(file);
	}
}
